/**
 * 游戏组件定义
 */
import { Component } from './ecs';

/**
 * 变换组件 - 位置、旋转、速度
 */
export interface Transform extends Component {
  x: number;
  y: number;
  rotation: number;
  velocityX: number;
  velocityY: number;
}

export const Transform = (x = 0, y = 0, rotation = 0): Transform => ({
  x, y, rotation, velocityX: 0, velocityY: 0
});

/**
 * 渲染组件 - 外观
 */
export interface Renderer extends Component {
  color: string;
  radius: number;
  shape: 'circle' | 'triangle' | 'square';
}

export const Renderer = (
  color = '#00ffff', 
  radius = 16, 
  shape: 'circle' | 'triangle' | 'square' = 'circle'
): Renderer => ({
  color, radius, shape
});

/**
 * 碰撞体组件
 */
export interface Collider extends Component {
  radius: number;
  isTrigger: boolean;
  layer: string;
}

export const Collider = (
  radius = 16, 
  isTrigger = false, 
  layer = 'default'
): Collider => ({
  radius, isTrigger, layer
});

/**
 * 生命值组件
 */
export interface Health extends Component {
  current: number;
  max: number;
}

export const Health = (max = 100): Health => ({
  current: max, max
});

/**
 * 武器组件
 */
export interface Weapon extends Component {
  fireRate: number; // 射击间隔（秒）
  lastFireTime: number;
  damage: number;
  bulletSpeed: number;
  bulletColor: string;
}

export const Weapon = (
  fireRate = 0.3,
  damage = 25,
  bulletSpeed = 800,
  bulletColor = '#ffff00'
): Weapon => ({
  fireRate, lastFireTime: 0, damage, bulletSpeed, bulletColor
});

/**
 * 子弹组件
 */
export interface Bullet extends Component {
  damage: number;
  owner: 'player' | 'enemy';
  lifeTime: number;
  maxLifeTime: number;
}

export const Bullet = (
  damage = 25,
  owner: 'player' | 'enemy' = 'player',
  maxLifeTime = 3
): Bullet => ({
  damage, owner, lifeTime: 0, maxLifeTime
});

/**
 * 敌机AI组件
 */
export interface EnemyAI extends Component {
  type: 'scout' | 'destroyer' | 'mothership';
  movePattern: 'straight' | 'zigzag' | 'circle';
  targetX?: number;
  targetY?: number;
  patternTime: number;
}

export const EnemyAI = (
  type: 'scout' | 'destroyer' | 'mothership' = 'scout',
  movePattern: 'straight' | 'zigzag' | 'circle' = 'straight'
): EnemyAI => ({
  type, movePattern, patternTime: 0
});

/**
 * 粒子组件
 */
export interface Particle extends Component {
  lifeTime: number;
  maxLifeTime: number;
  startSize: number;
  endSize: number;
  startAlpha: number;
  endAlpha: number;
  gravity: number;
}

export const Particle = (
  maxLifeTime = 1,
  startSize = 4,
  endSize = 0,
  startAlpha = 1,
  endAlpha = 0,
  gravity = 0
): Particle => ({
  lifeTime: 0, maxLifeTime, startSize, endSize, startAlpha, endAlpha, gravity
});

/**
 * 波次管理组件
 */
export interface WaveManager extends Component {
  currentWave: number;
  enemiesRemaining: number;
  waveStartTime: number;
  timeBetweenWaves: number;
  isWaveActive: boolean;
}

export const WaveManager = (): WaveManager => ({
  currentWave: 1,
  enemiesRemaining: 0,
  waveStartTime: 0,
  timeBetweenWaves: 3,
  isWaveActive: false
});

/**
 * 游戏状态组件
 */
export interface GameState extends Component {
  score: number;
  lives: number;
  gameOver: boolean;
  paused: boolean;
}

export const GameState = (): GameState => ({
  score: 0,
  lives: 3,
  gameOver: false,
  paused: false
});
